<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import type { ConsultOrderPreData } from '@/types/consult'
import { getOrderPre, createConsultOrder } from '@/api/consult'
import type { Patient } from '@/types/user'
import { getPatientInfo } from '@/api/user'
import { useConsultStore } from '@/stores'
import { useRouter } from 'vue-router'
import { showToast, showConfirmDialog } from 'vant'
import { useOrderStore } from '@/stores'
const router = useRouter()
const store = useConsultStore()
const orderStore = useOrderStore()
const orderPre = ref({} as ConsultOrderPreData)
const patient = ref({} as Patient)
onMounted(async () => {
  if (!store.consult.type) {
    // push & replace
    router.replace('/')
    return
  }

  // 获取预付单信息
  const res = await getOrderPre({
    type: store.consult.type,
    illnessType: store.consult.illnessType
  })
  orderPre.value = res.data

  // 获取患者信息
  const res2 = await getPatientInfo(store.consult.patientId)
  patient.value = res2.data
})

// 下单和支付
const agree = ref(true)
// let orderId = ''

// const paySheetRef = ref()

const show = ref(false)
const orderAndPay = async () => {
  if (!agree.value) return showToast('必须同意支付协议!')
  try {
    const res = await createConsultOrder(store.consult)

    // 存储到仓库中
    orderStore.setOrderId(res.data.id)

    // orderId = res.data.id
    // console.log('---orderId---', orderId)
    // 展示支付面板（给用户去选择是微信支付还是支付宝支付）
    show.value = true
    // paySheetRef.value.show = true

    // 本地的仓库中存储的问诊信息清除掉
    store.clear()
  } catch (error) {
    console.log('---error---', error)
  }
}

// 关闭ActionSheet前
const beforeClose = () => {
  showConfirmDialog({
    title: '关闭支付',
    message: '取消支付将无法获得医生回复，医生接诊名额有限，是否确认关闭',
    confirmButtonText: '继续支付',
    cancelButtonText: '仍要关闭'
  })
    .then(() => {
      // on confirm
      return true
    })
    .catch(() => {
      // on cancel
      router.replace('/user/consult')
      return true
    })
}
</script>

<template>
  <div class="" v-if="orderPre.payment > 0">
    <cp-nav-bar title="支付"></cp-nav-bar>
    <div class="pay-info">
      <p class="tit">图文问诊 {{ orderPre.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${orderPre.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${orderPre.pointDeduction}`" />
      <van-cell title="实付款" :value="`-¥${orderPre.actualPayment}`" class="pay-price" />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell
        title="患者信息"
        :value="`${patient.name} | ${patient.genderValue} | ${patient.age}岁`"
      ></van-cell>
      <van-cell title="病情描述" :label="store.consult.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="agree">我已同意 <span class="text">支付协议</span></van-checkbox>
    </div>
    <van-submit-bar
      button-type="primary"
      :price="orderPre.actualPayment * 100"
      button-text="立即支付"
      text-align="left"
      @click="orderAndPay"
    />
    <!-- <cp-pay-sheet
      :actual-payment="orderPre.actualPayment"
      :before-close="beforeClose"
      ref="paySheetRef"
    /> -->
    <cp-pay-sheet
      :actual-payment="orderPre.actualPayment"
      :before-close="beforeClose"
      v-model:show="show"
      pay-callback="/room"
    />
  </div>
</template>

<style lang="scss" scoped>
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
</style>
